package com.hxd.otc.main;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.constraint.ConstraintLayout;
import android.support.v4.content.ContextCompat;
import android.support.v4.util.SparseArrayCompat;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hxd.otc.R;
import com.hxd.otc.common.UserHelper;
import com.hxd.otc.main.bean.TabBottomBean;
import com.hxd.otc.module.login.activity.LoginActivity;

import java.util.List;

/**
 * 作  者 :  胡庆岭
 * 时  间 :  2018/6/1 17:46
 * 描  述 :  ${Main主页底部Adapter}
 */
public class TabBottomAdapter extends RecyclerView.Adapter<TabBottomAdapter.ViewHolder> {

    private Context mContext;
    private List<TabBottomBean> mData;
    private SparseArrayCompat<Boolean> mArrayCompat;
    private int mBeforePosition;
    boolean isVis;//订单是否显示红点

    //通知订单红点显示
    public void setVis(boolean vis) {
        isVis = vis;
        notifyDataSetChanged();
    }

    public void setOnRecyclerTabClickListener(OnRecyclerTabClickListener onRecyclerTabClickListener) {
        this.mOnRecyclerTabClickListener = onRecyclerTabClickListener;
    }

    private OnRecyclerTabClickListener mOnRecyclerTabClickListener;


    public TabBottomAdapter(@NonNull Context context, @NonNull List<TabBottomBean> data) {
        this.mContext = context;
        this.mData = data;
        initCheckMap();
    }

    private void initCheckMap() {
        mArrayCompat = new SparseArrayCompat<>();
        if (mData != null && mData.size() > 0) {
            for (int i = 0; i < mData.size(); i++) {
                mArrayCompat.put(i, i == 0);
            }
        }

    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_main_bottom_view, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, @SuppressLint("RecyclerView") final int position) {
        if (mArrayCompat.get(position)) {
            mBeforePosition = position;
        }
        if (position == 1 && isVis) {
            holder.tvRed.setVisibility(View.VISIBLE);
        }else {
            holder.tvRed.setVisibility(View.GONE);
        }

        holder.mItemCheck.setText(mData.get(position).getTabName());
        holder.mItemCheck.setChecked(mArrayCompat.get(position));
        holder.mItemCheck.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(mContext, mData.get(position).getTabRes()), null, null);
        holder.mParentLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onTabClick(v, position);


            }
        });
    }

    public void onTabClick(View v, @SuppressLint("RecyclerView") int position) {
        if (position == 1) {
            if (!UserHelper.isLogin()) {
                mContext.startActivity(new Intent(mContext, LoginActivity.class));
                mArrayCompat.put(mBeforePosition, true);
                mOnRecyclerTabClickListener.clickTab(v, mBeforePosition);
            } else {
                checkPosition(v, position);
            }
        } else {
            checkPosition(v, position);
        }
        notifyDataSetChanged();
    }

    public void checkPosition(View v, int position) {
        if (!mArrayCompat.get(position)) {
            for (int i = 0; i < mArrayCompat.size(); i++) {
                if (position == i) {
                    mArrayCompat.put(position, true);
                } else {
                    mArrayCompat.put(i, false);
                }
            }
        }
        if (mOnRecyclerTabClickListener != null) {
            mOnRecyclerTabClickListener.clickTab(v, position);
        }
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private CheckBox mItemCheck;
        private ConstraintLayout mParentLayout;
        private TextView tvRed;

        public ViewHolder(View itemView) {
            super(itemView);
            initView(itemView);
        }

        private void initView(View itemView) {
            mParentLayout = itemView.findViewById(R.id.item_parent_ll);
            tvRed = itemView.findViewById(R.id.tv_red);
            if (mData != null && mData.size() > 0) {
                ViewGroup.LayoutParams layoutParams = mParentLayout.getLayoutParams();
                layoutParams.height = RelativeLayout.LayoutParams.MATCH_PARENT;
                WindowManager windowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
                if (windowManager == null)
                    return;

                DisplayMetrics dm = new DisplayMetrics();
                windowManager.getDefaultDisplay().getMetrics(dm);
                windowManager.getDefaultDisplay();
                layoutParams.width = dm.widthPixels / mData.size();
                mParentLayout.setLayoutParams(layoutParams);
            }
            mItemCheck = itemView.findViewById(R.id.item_cb);
        }

    }


    public interface OnRecyclerTabClickListener {
        void clickTab(View view, int position);
    }

    public int getBeforePosition() {
        return mBeforePosition;
    }
}
